__author__ = 'Muscle'
import weakref

from mui.muicomponent import CommandBase, MUIComponent, CaptureObject, MUIStatic
from mui.Widgets.muipanel import MUIStackPanel
from mui.Widgets.muicanvas import MUICanvas
from mui.Widgets.muibutton import MUIIconButton
from mui.Widgets.muitooltips import TipBase
from mui.Widgets.muitext import TextBase
from mui.CExt.CW32Window import *

class MUICaptionbar(MUICanvas):
    """
    The caption bar can move or fullscreen the koan window.
    It does not have to be a direct child of window.
    """
    def __init__(self, parent=None, **argd):
        MUICanvas.__init__(self, parent, **argd)
        self.enableFullscreen = True
        self.bind('Mouse Down', self.__onMouseDown)
        self.bind('Dbl Click', self.__onDblClick)

    def __onDblClick(self, comp, x, y, buttons, modifiers):
        if self.window and self.enableFullscreen:
            self.window.setFullScreen(True, True, True)

    def __onMouseDown(self, comp, x, y, buttons, modifiers):
        if self.window:
            self.window.enterMovingMode()

class MUICaption(MUICanvas, CaptureObject, TextBase):
    """
    The caption can move the dialog or child window (within its parent).
    It does not have to be a direct child of dialog (if not, set 'moveTarget = dialog').
    """
    def __init__(self, parent=None, **argd):
        MUICanvas.__init__(self, parent, **argd)
        CaptureObject.__init__(self)
        TextBase.__init__(self, **argd)
        self.useGlobalCapture = True

        self.__moveTarget = weakref.ref(self.parent)
        self.__capX, self.__capY = 0, 0
        self.caption = argd.get('caption', '')

        self.bind('Capture Begin', self.__onBeginMove, postevent = False)
        self.bind('Capture Offset', self.__onMove, postevent = False)
        self.bind('Capture End', self.__onEndMove, postevent = False)

    def __setMoveTarget(self, target):
        self.__moveTarget = weakref.ref(target)
    def __getMoveTarget(self):
        return self.__moveTarget()
    moveTarget = property(__getMoveTarget, __setMoveTarget)

    def __onBeginMove(self, _x, _y):
        if self.moveTarget:
            self.__capX, self.__capY = self.moveTarget.left, self.moveTarget.top
            self.window.invoke('Lost Win32Ctrl')

    def __onMove(self, x, y):
        if self.moveTarget and self.moveTarget.parent:
            l = self.__capX + x
            t = self.__capY + y
            self.moveTarget.left = min(max(l, 0), self.moveTarget.parent.width - self.moveTarget.width)
            self.moveTarget.top = min(max(t, 0), self.moveTarget.parent.height - self.moveTarget.height)

    def __onEndMove(self, _x, _y):
        if self.moveTarget:
            self.window.invoke('Restore Win32Ctrl')

    def __getCaption(self):
        return self.text

    def __setCaption(self, v):
        self.text = v

    caption = property(__getCaption, __setCaption)

    def on_draw(self, renderer):
        super(MUICaption, self).on_draw(renderer)
        TextBase.draw(self, renderer)